package cn.stylefeng.guns.modular.gridsystem.config;

import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.apache.activemq.xbean.BrokerFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;

/**
 * @author ssy
 * @date 2021/8/9 11:55
 */
@Component
public class ActiveMqConf {

    public static final String myQueue = "myQueue";

    public static final String thirdQueue = "thirdQueue";

    @Value("${spring.activemq.broker-url}")
    private String mqUrl;

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue);
    }

    @Bean
    public Queue thirdQueue(){
        return new ActiveMQQueue(thirdQueue);
    }

    @Bean
    public BrokerFactoryBean activemq() {
        BrokerFactoryBean broker = new BrokerFactoryBean();
        broker.setConfig(new ClassPathResource("activemq.xml"));
        broker.setStart(true);
        return broker;
    }

    @Bean
    public ActiveMQConnectionFactory createFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(mqUrl);
        factory.setUserName("admin");
        factory.setPassword("admin");
        RedeliveryPolicy policy = new RedeliveryPolicy();
        //是否在每次尝试重新发送失败后,增长这个等待时间
        policy.setUseExponentialBackOff(true);
        //第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value
        policy.setBackOffMultiplier(2);
        // 每隔几秒重试一次
        policy.setInitialRedeliveryDelay(30000);
        //重发时间间隔
        policy.setRedeliveryDelay(5000);
        //最大重发次数
        policy.setMaximumRedeliveries(3);
        //setUseExponentialBackOff = true时有效，假设首次重连间隔为10ms，倍数为2，那么第 二次重连时间间隔为 20ms，第三次重连时间间隔为40ms
        // 当重连时间间隔大的最大重连时间间隔时，以后每次重连时间间隔都为最大重连时间间隔
        policy.setMaximumRedeliveryDelay(10000);
        factory.setRedeliveryPolicy(policy);
        //设置对象可作为消息传递的包
        factory.setTrustAllPackages(true);
//        factory.setBrokerURL(springBootMQConfig.getBrokerUrl());
        return factory;
    }

    @Bean
    public JmsTemplate createJsmTemplate(ActiveMQConnectionFactory factory) {
        JmsTemplate template = new JmsTemplate(factory);
        template.setSessionTransacted(false);
        //设置ack为手动确认模式
        template.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
        template.setConnectionFactory(factory);
        template.setDeliveryMode(2);
        return template;
    }

    @Bean("jmsListenerContainerFactory")
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory factory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setSessionTransacted(false);
        bean.setPubSubDomain(false);
        bean.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
        bean.setConnectionFactory(factory);
        return bean;
    }

}
